<template>
    <div id="searchAlls">
        <h3 class="searchAllh3">找到约{{length}}条结果</h3>
        <div class='searchAll' v-for='content in contents' :key="content.aid">
            <router-link tag="h3" :to="{path:'/article',query:{aid:content.aid}}">{{content.title}}</router-link>
            <p class="p1">{{content.content}}</p>
            <p>{{content.likeClick}} 获赞&nbsp;&nbsp;&nbsp;{{content.response}} 评论</p>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return {
             query: '',
            //  title:'111',
             val:'',
             contents:'',
             length:0
            }
    },
    created(){
        this.query=this.$route.query.searchval,
        this.val='%'+this.query+'%'
        console.log(this.val)
            this.$axios.get('/api/search/topic',{params:{val:this.val}})
                .then(res=>{
                    console.log('topicres:',res)
                    this.contents=res.data.data
                    if(this.contents.length)
                        this.length=this.contents.length
                })
                .catch(err=>{
                    console.log('err:',err)
                })
    },
    watch: {
        $route(to,from){
            this.query=this.$route.query.searchval,
            this.val='%'+this.query+'%',
            this.$axios.get('/api/search/topic',{params:{val:this.val}})
                .then(res=>{
                    console.log('res:',res)
                    // this.author=res.data.data[0]
                    if(this.contents.length)
                        this.length=this.contents.length
                })
                .catch(err=>{
                    console.log('err:',err)
                })
              
        }
    },
}
</script>